import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import Qt


class KeyCaptureWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Key Capture Widget")
        self.setGeometry(100, 100, 300, 200)

    def keyPressEvent(self, event):
        # 当按下按键时触发此事件处理函数
        if event.key() == Qt.Key_Up:
            print("Up key pressed")
        elif event.key() == Qt.Key_Down:
            print("Down key pressed")
        elif event.key() == Qt.Key_Left:
            print("Left key pressed")
        elif event.key() == Qt.Key_Right:
            print("Right key pressed")
        elif event.key() == Qt.Key_Escape:  # 检测是否按下了 Escape 键
            print("Escape 键被按下")
            self.close()  # 按下 Escape 键关闭窗口
        elif event.key() == Qt.Key_Return:  # 检测是否按下了回车键
            print("回车键被按下")
        else:
            print(f"其他键被按下: {event.text()}, 键码: {event.key()}")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = KeyCaptureWidget()
    window.show()
    sys.exit(app.exec_())
